"""
本章节研究的使用session跟踪用户的身份的状态。
"""


from flask import Blueprint,request, render_template, redirect,session
# 创建蓝图
session_blue = Blueprint("session", __name__)
"""
开发流程:
  
  
"""


# 首页
@session_blue.route("/session-home/")
def session_hone():
    # 获取session
    username = session.get("user")
    # 渲染静态页面
    return render_template("session_home.html", username=username)


# 登录页面
@session_blue.route("/session-login/",methods=["GET","POST"])
def session_login():
    # 如果是get请求 则渲染登录页面
    if request.method == "GET":
        return render_template("session_login.html")
    elif request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")
        if username == "lisi" and password == "1234":
            response = redirect("/session-home/")
            session["user"] = username
            return response

    else:
        return "用户名或者密码错误"


# 登出页面
@session_blue.route("/session-logout/")
def session_logout():
    # 删除session
    response=session.pop("user")
    return response
